﻿#pragma once

#include "MLibrary.h"
#include "../MirControls/MirControl.h"
#include "../../Shared/Enums.h"
#include <vector>

using namespace Client::MirControls;
using namespace Client::MirScenes;
using namespace SlimDX;
using namespace SlimDX::Direct3D9;
//C# TO C++ CONVERTER TODO TASK: C# to C++ Converter could not confirm whether this is a namespace alias or a type alias:
//using Blend = SlimDX::Direct3D9::Blend;

namespace Client::MirGraphics
{
    class DXManager
    {
    public:
        static std::vector<MImage*> TextureList;
        static std::vector<MirControl*> ControlList;


        static Device *Device;
        static Sprite *Sprite;
        static Sprite *TextSprite;
        static Line *Line;

        static Surface *CurrentSurface;
        static Surface *MainSurface;
        static PresentParameters *Parameters;
        static bool DeviceLost;
        static float Opacity;
        static bool Blending;
        static float BlendingRate;
        static BlendMode BlendingMode;


        static Texture *RadarTexture;
        static std::vector<Texture*> Lights;
        static Texture *PoisonDotBackground;

        static Texture *FloorTexture;
        static Texture *LightTexture;
        static Surface *FloorSurface;
        static Surface *LightSurface;

        static PixelShader *GrayScalePixelShader;
        static PixelShader *NormalPixelShader;
        static PixelShader *MagicPixelShader;
        static PixelShader *ShadowPixelShader;

        static bool GrayScale;

        static std::vector<Point*> LightSizes;

        static void Create();

//C# TO C++ CONVERTER TODO TASK: C# 'unsafe' code is not converted by C# to C++ Converter:
//        private static unsafe void LoadPixelsShaders()
//        {
//            var shaderNormalPath = Settings.ShadersPath + "normal.ps";
//            var shaderGrayScalePath = Settings.ShadersPath + "grayscale.ps";
//            var shaderMagicPath = Settings.ShadersPath + "magic.ps";
//
//            if (System.IO.File.Exists(shaderNormalPath))
//            {
//                using (var gs = ShaderBytecode.AssembleFromFile(shaderNormalPath, ShaderFlags.None))
//                    NormalPixelShader = new PixelShader(Device, gs);
//            }
//            if (System.IO.File.Exists(shaderGrayScalePath))
//            {
//                using (var gs = ShaderBytecode.AssembleFromFile(shaderGrayScalePath, ShaderFlags.None))
//                    GrayScalePixelShader = new PixelShader(Device, gs);
//            }
//            if (System.IO.File.Exists(shaderMagicPath))
//            {
//                using (var gs = ShaderBytecode.AssembleFromFile(shaderMagicPath, ShaderFlags.None))
//                    MagicPixelShader = new PixelShader(Device, gs);
//            }
//        }

//C# TO C++ CONVERTER TODO TASK: C# 'unsafe' code is not converted by C# to C++ Converter:
//        private static unsafe void LoadTextures()
//        {
//            Sprite = new Sprite(Device);
//            TextSprite = new Sprite(Device);
//            Line = new Line(Device) { Width = 1F };
//
//            MainSurface = Device.GetBackBuffer(0, 0);
//            CurrentSurface = MainSurface;
//            Device.SetRenderTarget(0, MainSurface);
//
//            if (RadarTexture == nullptr || RadarTexture.Disposed)
//            {
//                RadarTexture = new Texture(Device, 2, 2, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
//
//                DataRectangle stream = RadarTexture.LockRectangle(0, LockFlags.Discard);
//                using (Bitmap image = new Bitmap(2, 2, 8, PixelFormat.Format32bppArgb, stream.Data.DataPointer))
//                using (Graphics graphics = Graphics.FromImage(image))
//                    graphics.Clear(Color.White);
//            }
//            if (PoisonDotBackground == nullptr || PoisonDotBackground.Disposed)
//            {
//                PoisonDotBackground = new Texture(Device, 5, 5, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
//
//                DataRectangle stream = PoisonDotBackground.LockRectangle(0, LockFlags.Discard);
//                using (Bitmap image = new Bitmap(5, 5, 20, PixelFormat.Format32bppArgb, stream.Data.DataPointer))
//                using (Graphics graphics = Graphics.FromImage(image))
//                    graphics.Clear(Color.White);
//            }
//            CreateLights();
//        }

//C# TO C++ CONVERTER TODO TASK: C# 'unsafe' code is not converted by C# to C++ Converter:
//        private unsafe static void CreateLights()
//        {
//
//            for (int i = Lights.Count - 1; i >= 0; i--)
//                Lights[i].Dispose();
//
//            Lights.Clear();
//
//            for (int i = 1; i < LightSizes.Length; i++)
//            {
//                // int width = 125 + (57 *i);
//                //int height = 110 + (57 * i);
//                int width = LightSizes[i].X;
//                int height = LightSizes[i].Y;
//
//                Texture light = new Texture(Device, width, height, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
//
//                DataRectangle stream = light.LockRectangle(0, LockFlags.Discard);
//                using (Bitmap image = new Bitmap(width, height, width * 4, PixelFormat.Format32bppArgb, stream.Data.DataPointer))
//                {
//                    using (Graphics graphics = Graphics.FromImage(image))
//                    {
//                        using (GraphicsPath path = new GraphicsPath())
//                        {
//                            //path.AddEllipse(new Rectangle(0, 0, width, height));
//                            //using (PathGradientBrush brush = new PathGradientBrush(path))
//                            //{
//                            //    graphics.Clear(Color.FromArgb(0, 0, 0, 0));
//                            //    brush.SurroundColors = new[] { Color.FromArgb(0, 255, 255, 255) };
//                            //    brush.CenterColor = Color.FromArgb(255, 255, 255, 255);
//                            //    graphics.FillPath(brush, path);
//                            //    graphics.Save();
//                            //}
//
//                            path.AddEllipse(new Rectangle(0, 0, width, height));
//                            using (PathGradientBrush brush = new PathGradientBrush(path))
//                            {
//                                Color[] blendColours = { Color.White, Color.FromArgb(255,150,150,150), Color.FromArgb(255,60,60,60), Color.FromArgb(255,30,30,30), Color.FromArgb(255,10,10,10), Color.FromArgb(0,0,0,0)};
//
//                                float[] radiusPositions = { 0f, .20f, .40f, .60f, .80f, 1.0f };
//
//                                ColorBlend colourBlend = new ColorBlend();
//                                colourBlend.Colors = blendColours;
//                                colourBlend.Positions = radiusPositions;
//
//                                graphics.Clear(Color.FromArgb(0, 0, 0, 0));
//                                brush.InterpolationColors = colourBlend;
//                                brush.SurroundColors = blendColours;
//                                brush.CenterColor = Color.White;
//                                graphics.FillPath(brush, path);
//                                graphics.Save();
//                            }
//                        }
//                    }
//                }
//                //light.Disposing += (o, e) => Lights.Remove(light);
//                Lights.Add(light);
//            }
//        }

        static void SetSurface(Surface *surface);
        static void SetGrayscale(bool value);

        static void AttemptReset();

        static void ResetDevice();

        static void AttemptRecovery();
        static void SetOpacity(float opacity);
        static void SetBlend(bool value, float rate = 1.0F, BlendMode mode = BlendMode::NORMAL);

        static void SetNormal(float blend, Color *tintcolor);

        static void SetGrayscale(float blend, Color *tintcolor);

        static void SetBlendMagic(float blend, Color *tintcolor);

        static void Clean();


    private:
        static void CleanUp();

    };
}
